<?php
namespace Home\Controller;

use Org\Util\Snoopy;
use Think\Controller;

class IndexController extends Controller
{
    public function _initialize(){
        header("content-type:text/html; charset=utf-8");
    }

    public function index()
    {
        //关闭模板
        layout(false);


        $sn=new Snoopy();

        $url="http://jw.gxsdxy.cn/jiaowures/Login.aspx";

       // $jwos=D("JwIp");
       // $ipnum=$jwos->count();

       // $No=rand(1,$ipnum);

       // $ret=$jwos->getItem($No);

        $ret=array(
            "ip"=>"120.52.72.38",
            "port"=>"80"
        );

       // dump($ret);

        session("ip",$ret);
 //       dump(session("ip"));
        $sn->proxy_host=session("ip.ip");
        $sn->proxy_port=session("ip.port");

//        $sn->proxy_host="59.39.88.190";
//        $sn->proxy_port="8080";

//        dump($ret);


        $sn->fetch($url);
        $jw=$sn->results;

//        echo $jw;
//        echo substr(sprintf('%o',fileperms(session_save_path())),-4);
        $pattern = '/<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" \/>/is';
        preg_match_all($pattern, $jw, $matches);
        $res[0] = $matches[1][0];
        $pattern = '/<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*?)" \/>/is';
        preg_match_all($pattern, $jw, $matches);
        $res[1] = $matches[1][0];


        // if(empty($res[1])){
        // 	$this->redirect("index");
        // }

//        dump($sn->headers);
        //$c=$sn->headers[8];
        //preg_match_all('|Set-Cookie: ASP.NET_SessionId=(.*);|U',$c,$mycookie);
        
        $c=$sn->headers;
//        dump($sn->headers);
        for($i=0;$i<count($c);$i++){
            preg_match_all('|Set-Cookie: ASP.NET_SessionId=(.*);|U',$c[$i],$mycookie);
            if(!empty($mycookie[1][0])){
                break;
            }
        }

        $sn2=new Snoopy;

        $sn2->proxy_host=session("ip.ip");
        $sn2->proxy_port=session("ip.port");
//        dump(session("ip"));
//        echo session("ip.ip");
        $sn2->cookies['ASP.NET_SessionId']=$mycookie[1][0];
//print_r($mycookie);
        $sn2->agent="Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko";
//$sn->referer="http://jw.gxsdxy.cn/jiaowu/";
        $sn2->rawheaders['Pragma']='no-cache';
        $sn2->fetch("http://jw.gxsdxy.cn/jiaowures/other/CheckCode.aspx?datetime=az");
        $verifyjpg = time();
//        echo $sn2->results;
        file_put_contents(TEMP_PATH."{$verifyjpg}.jpg",$sn2->results);

        $dataInfo=array(
            "VIEWSTATE"=>$res[0],
            "EVENTVALIDATION"=>$res[1],
            "Cookie"=>$mycookie[1][0],
            "imgpath"=>TEMP_PATH."{$verifyjpg}.jpg"
        );

//        dump($dataInfo);
        $this->assign("dataInfo",$dataInfo);
        $this->display();
    }

    public function login(){
        $sn=new Snoopy;

        $url="http://jw.gxsdxy.cn/jiaowures/Login.aspx";

        $post['__VIEWSTATE']=I("post.__VIEWSTATE");
        $post['__EVENTVALIDATION']=I("post.__EVENTVALIDATION");
        $post['Account']=I("post.Account");
        $post['PWD']=I("post.PWD");
        $post['CheckCode']=I("post.CheckCode");
        $post['cmdok']="";

//        session("Account",$post['Account']);

        $_SESSION["stuid"]=$post['Account'];

        $verifyjpg=I("post.img");
        unlink($verifyjpg);

        $sn->proxy_host=session("ip.ip");
        $sn->proxy_port=session("ip.port");

        $sn->cookies['ASP.NET_SessionId']=I("post.Cookie");
//    print_r($_POST['Cookie']);
        $sn->agent="Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko";
        $sn->referer="http://jw.gxsdxy.cn/jiaowures/";
//        $sn->rawheaders['X_FORWARDED_FOR'] = "jw.gxsdxy.cn";
        $sn->rawheaders['Pragma']='no-cache';

        $sn->submit($url,$post);
        $c=$sn->cookies;
//        $_SESSION['cookies']=$c;
        session("cookies",$c);

        $sn2 = new Snoopy;

        $sn2->proxy_host=session("ip.ip");
        $sn2->proxy_port=session("ip.port");

        $sn2->cookies=$c;
        $sn2->agent = "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko";
        $sn2->referer = "http://jw.gxsdxy.cn/jiaowures/";
        $sn2->rawheaders['Pragma'] = 'no-cache';
//        $sn2->rawheaders['X_FORWARDED_FOR'] = "jw.gxsdxy.cn";
//        $sn->fetch("http://jw.gxsdxy.cn/jiaowures/JWXS/Default.aspx");
        $sn2->maxredirs=0;
        if($sn2->fetch("http://jw.gxsdxy.cn/jiaowures/JWXS/Default.aspx")){
            if($sn2->status==200){
                session("Account",$post['Account']);

                $pattern = '/<span id="users">(.*?)<\/span>/is';
                preg_match_all($pattern, mb_convert_encoding($sn2->results,"utf-8","gb2312"), $name);
                session("name",$name[1][0]);

                $this->redirect("Jw/index");
            }else{
                $this->error('登录失败，请重新登录！');
//                dump($post);
            }


        }else{
            echo "网络错误，稍后再登录。或联系敏捷软件协会，微信公众号：mjrjxh <br>".$sn2->error;
        }

    }

    public function logout(){
        session(null);
        $this->redirect("index");
    }
}
